package com.ithema.reggie.controller;

import com.ithema.reggie.common.R;
import com.ithema.reggie.entity.Category;
import com.ithema.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("page")
    public R findByPage(@RequestParam("page") Integer pageNum, Integer pageSize){
        //调用service完成分页查询操作
        return categoryService.findByPage(pageNum,pageSize);
    }

    /**
     * 添加分类
     * @param category
     * @param session
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category, HttpSession session){

        //给category封装操作的用户id
        Long userId = (Long) session.getAttribute("employee");
        category.setCreateUser(userId);
        category.setUpdateUser(userId);

        //调用service完成保存
        return categoryService.save(category);
    }
    /**
     * 作用：修改类别
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category,HttpSession session){

        //给category封装操作的用户id
        Long userId = (Long) session.getAttribute("employee");
        category.setUpdateUser(userId);

        //调用service完成更新操作
        return categoryService.update(category);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delet(Long id){
        categoryService.removeById(id);
        return R.success("分类信息删除成功");
    }

    /**
     *  类别列表
     * @param type
     * @return
     */
    @GetMapping("list")
    //通过type查询分类信息
    public R findAllByType(Integer type){
        return categoryService.findAllByType(type);
    }
}
